package com.pencilisland.pay.mgt.app.controller;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.document.AbstractXlsView;

import com.pencilisland.pay.mgt.app.entity.OrderInfoEntity;
import com.pencilisland.pay.mgt.app.service.OrderService;

@Controller
@RequestMapping(value = "/app/order")
public class OrderController {
	@Autowired
	OrderService orderService;

	/**
	 * 进入订单管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "")
	public String toOrderList() {
		return "app/order";
	}

	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> orderList(@ModelAttribute OrderInfoEntity cretiria,
			@RequestParam(value = "start") int start, @RequestParam(value = "length") int length) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		Page<OrderInfoEntity> orderPage = orderService.orderList(cretiria, start, length);

		resultMap.put("recordsFiltered", orderPage.getTotalElements());
		resultMap.put("recordsTotal", orderPage.getTotalElements());
		resultMap.put("orderList", orderPage.getContent());

		return resultMap;
	}
	
	@RequestMapping(value = "/download/excel/kuaidi100", method = RequestMethod.POST)
	public View downExcelForKuaidi100(@ModelAttribute final OrderInfoEntity cretiria,HttpServletRequest request) {
		return new AbstractXlsView() {
			@Override
			protected Workbook createWorkbook(Map<String, Object> model, HttpServletRequest request){
				try {
					return new HSSFWorkbook(new FileInputStream(ResourceUtils.getFile("classpath:excel/kuaidi100import.xls")));
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				return new HSSFWorkbook();
			} 
			@Override
			protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
					HttpServletResponse response) throws Exception {
				
				orderService.buildKuaidi100Excel(workbook,cretiria);
				
				String fileName = URLEncoder.encode("快递100导入订单.xls", "UTF-8");
				response.setHeader("Content-disposition", "attachment;filename=" + fileName);
			}
		};
	}
	
	/**
	 * 导出中通
	 * @param cretiria
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/download/excel/zhongtong", method = RequestMethod.POST)
	public View downExcelForZhongtong(@ModelAttribute final OrderInfoEntity cretiria,HttpServletRequest request) {
		return new AbstractXlsView() {
			@Override
			protected Workbook createWorkbook(Map<String, Object> model, HttpServletRequest request){
				try {
					return new HSSFWorkbook(new FileInputStream(ResourceUtils.getFile("classpath:excel/zhongtong_import.xls")));
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				return new HSSFWorkbook();
			} 
			@Override
			protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
					HttpServletResponse response) throws Exception {
				
				orderService.buildZhongtongExcel(workbook,cretiria);
				
				String fileName = URLEncoder.encode("中通导入订单.xls", "UTF-8");
				response.setHeader("Content-disposition", "attachment;filename=" + fileName);
			}
		};
	}

	/**
	 * 撤销订单
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/cancel")
	@ResponseBody
	public Map<String, Object> cancel(@RequestParam(value = "orderNos", required = true) String orderNos,
			@RequestParam(value = "remark", required = false) String remark) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			orderService.cancel(orderNos,remark);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}
	
	/**
	 * 订单发货
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/send")
	@ResponseBody
	public Map<String, Object> send(@RequestParam(value = "orderNos", required = true) String orderNos,
			@RequestParam(value = "trackingNumber", required = false) String trackingNumber,
			@RequestParam(value = "remark", required = false) String remark) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			orderService.send(orderNos,trackingNumber,remark);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}

	/**
	 * 加载数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/load/{orderNo}")
	@ResponseBody
	public Map<String, Object> load(@PathVariable(value = "orderNo") String orderNo) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			resultMap.put("data", orderService.loadData(orderNo));
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}
	
	/**
	 * 导入中通返回数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/import/zhongtong")
	@ResponseBody
	public Map<String, Object> importZhongtong(@RequestParam(value="dataFile")MultipartFile file) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			orderService.importZhongtong(file);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}

}
